/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package domain.threading;

import domain.DomainController;
import domain.forumdata.Forum;
import domain.forumdata.Post;
import java.util.ArrayList;
import java.util.logging.Level;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 *
 * This class runs on a separate thread.
 */
public class BatchPosterStarter extends Thread
{
    /**
     * Executes the thread.
     */
    @Override
    public void run()
    {
        DomainController domainController = DomainController.getInstance();
        domainController.getUi().setButtonsEnabled(false);
        domainController.getUi().updateProcess(0, "Posting...");

        try
        {
            //Selected forums...
            ArrayList<Forum> theSelectedForums = new ArrayList<Forum>();
            for(String s : domainController.getSelectedForums())
            {
                theSelectedForums.add(domainController.getForums().getForumByName(s));
            }

            //Start posting one by one...
            for(Post post : domainController.getQueue().getQueue())
            {
                PosterStarter ps = new PosterStarter(post, theSelectedForums, true);
                ps.post();
            }
        }
        catch(Exception e)
        {
            domainController.getLogger().log(Level.SEVERE, e.getMessage());
        }
        finally
        {
            //Done
            domainController.updateProgress(100, "Ready");
            domainController.getUi().setButtonsEnabled(true);
            domainController.loadHistoryUI();
        }
    }
}